package com.example.userservice.commont;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

@RestController
public class CommentController {
    @Autowired
    private KafkaTemplate<String, byte[]> kafkaTemplate;

    @PostMapping("/comments")
    public void createComment(@RequestBody CommentMessage request) {
        // 处理评论逻辑
        // ...


        // 发送评论消息
        // key 为 userIdB，value 为 CommentMessage 对象
        kafkaTemplate.send("secondTopic", request.getUserIdB(), request.getComment().getBytes(StandardCharsets.UTF_8));
    }

}